package com.aicode.controller;

import com.aicode.common.Result;
import com.aicode.model.Page;
import com.aicode.service.PageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pages")
public class PageController {
    
    @Autowired
    private PageService pageService;
    
    @GetMapping("/list")
    public Result<IPage<Page>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        PageDTO<Page> page = new PageDTO<>(current, size);
        return Result.success(pageService.page(page));
    }
    
    @PostMapping
    public Result<Page> create(@RequestBody Page page) {
        pageService.save(page);
        return Result.success(page);
    }
    
    @PutMapping("/{id}")
    public Result<Page> update(@PathVariable Long id, @RequestBody Page page) {
        page.setId(id);
        pageService.updateById(page);
        return Result.success(page);
    }
    
    @GetMapping("/{id}")
    public Result<Page> getById(@PathVariable Long id) {
        return Result.success(pageService.getById(id));
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        pageService.removeById(id);
        return Result.success(null);
    }
}